﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace DreamExplanation
{
    public partial class SubCategoryPage : PhoneApplicationPage
    {
        public SubCategoryPage()
        {
            InitializeComponent();
        }

        private string categoryName = null;
        private string contentSearch = null;
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            
            //retrieve pass in parameter
            categoryName = NavigationContext.QueryString["categoryName"];
            contentSearch = NavigationContext.QueryString["contentSearch"];

            if (!string.IsNullOrEmpty(categoryName) 
                && string.IsNullOrEmpty(contentSearch))
            {
                GenerateDataByCategory();
            }
            else if (!string.IsNullOrEmpty(contentSearch)
                && App.contentKeyWrodList!=null
                && App.contentKeyWrodList.Count()>0)
            {
                lbxCarmakers.ItemsSource = App.contentKeyWrodList;
            }
        }

        private void GenerateDataByCategory()
        {
            //generate binding data
            var dreamCategory = (from car in App.dreamCategories.Elements("DreamCategory")
                                 where car.Element("Name") != null
                                 && car.Element("Name").Value == categoryName
                                 select car).FirstOrDefault();

            if (dreamCategory != null)
            {
                if (dreamCategory.Element("Dreams") != null
                    && dreamCategory.Element("Dreams").Elements("DreamEntity") != null
                    && dreamCategory.Element("Dreams").Elements("DreamEntity").Count() > 0)
                {
                    var dreamContent = from dream in dreamCategory.Element("Dreams").Elements("DreamEntity")
                                       where dream.Element("Title") != null
                                       select dream.Element("Title").Value;
                    lbxCarmakers.ItemsSource = dreamContent;
                }

                //set page title
                if (dreamCategory.Element("Name") != null
                    && !string.IsNullOrEmpty(dreamCategory.Element("Name").Value))
                {
                    PageTitle.Text = dreamCategory.Element("Name").Value + " - 周公解梦";
                }
            }
        }

        private void lbxCarmakers_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (e != null
                && e.AddedItems != null
                && e.AddedItems.Count > 0
                && e.AddedItems[0] != null
                && e.AddedItems[0] is string
                && lbxCarmakers.SelectedIndex !=-1
                )
            {
                if (App.navigationInfos != null
                    && App.navigationInfos.Count() > 0)
                {
                    string subCategory = e.AddedItems[0] as string;

                    categoryName = (from o in App.navigationInfos
                                    where o.SubCategoryName == subCategory
                                    select o.CategoryName).FirstOrDefault();
                    if (!string.IsNullOrEmpty(categoryName))
                    {
                        string uri = string.Format("/Dreamcontent.xaml?categoryName={0}&&subCategoryName={1}&&contentSearch={2}", categoryName, subCategory, contentSearch);
                        lbxCarmakers.SelectedIndex = -1;
                        NavigationService.Navigate(new Uri(uri, UriKind.Relative));        
                    }
                }
            }
        }
    }
}